package cc.co.dreamind;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.*;

public class list extends ListActivity {
	public String db_name = "money.db";
	final dbHelper helper = new dbHelper(this, db_name, null, 1);
	ArrayList<String> al_data = new ArrayList<String>();
	ArrayList<Integer> al_id = new ArrayList<Integer>();
	ArrayList<Integer> al_amount = new ArrayList<Integer>();
	public int delete_id;
	public SQLiteDatabase db;
	
	AlertDialog.Builder sureToDelete;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// ====alert
		sureToDelete = new AlertDialog.Builder(this);
		sureToDelete.setMessage(list.this.getText(R.string.del_alert))
				.setNegativeButton(list.this.getText(R.string.Yes), new Ylistener())
				.setPositiveButton(list.this.getText(R.string.No), new Nlistener());
		// ================
		updateData();
		final String[] st_data1 = new String[al_data.size()];
		final Integer[] int_id1 = new Integer[al_id.size()];
		final String[] st_data = new String[al_data.size()];
		final Integer[] int_id = new Integer[al_id.size()];
		final Integer[] int_amount = new Integer[al_amount.size()];
		al_data.toArray(st_data1);
		al_id.toArray(int_id1);
		al_amount.toArray(int_amount);
		
		//reverse:
		for (int i = al_data.size() - 1; i >= 0; i--) {
			st_data[al_data.size() - i - 1] = st_data1[i];
			int_id[al_data.size() - i - 1] = int_id1[i];
		}
		
		//sum:
		int sum=0;
		for (int i = 0; i < al_amount.size(); i++){
			sum += int_amount[i];
		}
		this.setTitle(this.getText(R.string.total)+Integer.toString(sum));

		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_layout,
				st_data));
		ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO add alert for deleting the item;
				Toast.makeText(getApplicationContext(),
						((TextView) view).getText(), Toast.LENGTH_SHORT).show();
			}
		});

		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				// TODO haven't thought what to add here.

				delete_id = int_id[position];
				
				sureToDelete.show();

				return true;
			}

		});

	}

	// data

	public void updateData() {
		// read database to initial data[]
		Cursor c;
		c = helper.query("account1");
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			String str = c.getString(1) + "   " + c.getString(3)+"   "+c.getString(2);
			al_data.add(str);
			al_id.add(c.getInt(0));
			al_amount.add(c.getInt(3));
		}

	}

	class Ylistener implements DialogInterface.OnClickListener {

		public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(getApplicationContext(),
					list.this.getText(R.string.Toast_Delete), Toast.LENGTH_SHORT)
					.show();
			db = helper.getWritableDatabase();
			String cmd = "_id=" + Integer.toString(delete_id);
			db.delete("account1", cmd, null);
			
			list.this.finish();
			Intent intent = new Intent(list.this,list.class);
			list.this.startActivity(intent);
		}

	}

	class Nlistener implements DialogInterface.OnClickListener {

		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub

		}

	}
}
